
package com.yimixian.app.ui.dialog;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.R;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.AppConfig;
import com.yimixian.app.model.City;
import com.yimixian.app.model.KV;
import com.yimixian.app.model.Region;
import com.yimixian.app.ui.wheelview.OnWheelChangedListener;
import com.yimixian.app.ui.wheelview.WheelView;
import com.yimixian.app.ui.wheelview.adapter.ArrayWheelAdapter;
import com.yimixian.app.util.UiUtils;

import android.app.Activity;
import android.app.Dialog;
import android.database.DatabaseUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 轮子弹出框
 *
 * @author liuletao
 */
public class WheelViewDialog extends Dialog implements View.OnClickListener,
        OnWheelChangedListener {

    private boolean isSelctTwoWheel;
    public interface ClickSureListener {
        void onConfirm(KV kv);
        void onTwoSelectConfirm(String province, String city);
    }

    private ClickSureListener mClickSureListener;
    // 时间
    public List<KV> timeType = new ArrayList<KV>();
    private WheelView mViewItem, wheelViewProvince, wheelViewCity;
    Activity activity;
    Button btn_confirm, btn_cancel;

    /**
     * 所有选项
     */
    protected String[] mItems;
    /**
     * 用户选中的对象
     */
    private KV mUserChooseItem;
    /**
     * 默认选中的对象
     */
    private KV mChooseItem;

    /**
     * 二级列表数据源
     */
    List<Region> regions;
    private String SELECTED_PROVINCE;//选中的省份
    private String SELECTED_CITY;//选中的城市
    private String[] proviceItems;
    private String[] cityItems;
    private ArrayWheelAdapter cityArrayWheelAdapter;


    public WheelViewDialog(Activity activity, List<KV> timeType, KV mChooseItem) {
        super(activity, R.style.DialogStyle);
        isSelctTwoWheel = false;
        this.activity = activity;
        this.timeType = timeType;
        this.mChooseItem = mChooseItem;
        mItems = new String[timeType.size()];
        for (int i = 0; i < timeType.size(); i++) {
            mItems[i] = timeType.get(i).value;
        }
        initView();
    }

    /**
     * 二级列表选择
     * @param activity
     * @param regions 需要展示的一二级数据
     * @param province 选中的省份
     * @param city 选中的城市
     */
    public WheelViewDialog(Activity activity, List<Region> regions,
                           String province, String city){
        super(activity, R.style.DialogStyle);
        isSelctTwoWheel = true;
        this.activity = activity;
        this.regions = regions;
        this.SELECTED_PROVINCE = province;
        this.SELECTED_CITY = city;
        initTwoView();
    }

    /**
     * 二级初始化
     */
    public void initTwoView(){
        setContentView(R.layout.dialog_wheel_two);
        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth();
        mWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);

        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_confirm.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
        wheelViewProvince = (WheelView) findViewById(R.id.wheel_view_province);
        wheelViewCity = (WheelView) findViewById(R.id.wheel_view_city);
        // 添加change事件
        wheelViewProvince.addChangingListener(this);
        if (regions == null || regions.size() <=0) return;
        proviceItems = new String[regions.size()];
        int selectedP = 0;
        if (!StringUtils.isEmpty(SELECTED_CITY)) {
            AppConfig appConfig = DataManager.getInstance().get(CacheKeys.APP_CONFIG);
            selectedP = appConfig.getProvice(SELECTED_CITY);
            SELECTED_PROVINCE = regions.get(selectedP).name;
        }
        for (int i = 0; i < regions.size(); i++){
            proviceItems[i] = regions.get(i).name;
        }

        wheelViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(activity, proviceItems));
        // 设置可见条目数量
        wheelViewProvince.setVisibleItems(5);
        wheelViewProvince.setCyclic(false);

        wheelViewCity.addChangingListener(this);
        cityArrayWheelAdapter = new ArrayWheelAdapter<String>(activity, cityItems);
        wheelViewCity.setViewAdapter(cityArrayWheelAdapter);
        // 设置可见条目数量
        wheelViewCity.setVisibleItems(5);
        wheelViewCity.setCyclic(false);
        wheelViewProvince.setCurrentItem(selectedP);
        if (selectedP == 0) updateTwoCities(wheelViewProvince);
    }


    private void initView() {
        setContentView(R.layout.dialog_wheel);
        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth();
        mWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);

        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_confirm.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
        mViewItem = (WheelView) findViewById(R.id.wheel_view);
        // 添加change事件
        mViewItem.addChangingListener(this);
        mViewItem.setViewAdapter(new ArrayWheelAdapter<String>(activity, mItems));
        // 设置可见条目数量
        mViewItem.setVisibleItems(5);
        mViewItem.setCyclic(false);
        updateCities();
        // 从上个页面过来默认选中给定的选项
        if (mChooseItem != null && !StringUtils.isEmpty(mChooseItem.value)) {
            for (int i = 0; i < mItems.length; i++) {
                if (mChooseItem.value.equals(mItems[i])) {
                    mViewItem.setCurrentItem(i);
                    break;
                }
            }
        }
    }

    /** 获取屏幕分辨率宽 */
    public int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                if (mClickSureListener != null) {
                    if (isSelctTwoWheel)
                        mClickSureListener.onTwoSelectConfirm(SELECTED_PROVINCE, SELECTED_CITY);
                    else
                        mClickSureListener.onConfirm(mUserChooseItem);
                }
                dismiss();
                break;
            case R.id.btn_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }

    public void setmClickSureListener(ClickSureListener mClickSureListener) {
        this.mClickSureListener = mClickSureListener;
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewItem) {
            updateCities();
        }
        if (wheel == wheelViewProvince){
            updateTwoCities(wheelViewProvince);
        }
        if (wheel == wheelViewCity){
            int cityIndex = wheelViewCity.getCurrentItem();
            if (cityItems != null || cityItems.length > cityIndex)
                SELECTED_CITY = cityItems[cityIndex];
        }
    }

    private void updateCities() {
        int pCurrent = mViewItem.getCurrentItem();
        if (pCurrent >= timeType.size())
            return;
        mUserChooseItem = timeType.get(pCurrent);
    }

    private void updateTwoCities(WheelView wheel) {
        int pCurrent = wheel.getCurrentItem();
        if (pCurrent >= regions.size())
            return;
        SELECTED_PROVINCE = proviceItems[pCurrent];
        if (StringUtils.isEmpty(SELECTED_PROVINCE)) return;

        List<City> mCities = regions.get(pCurrent).cities;

        if (mCities == null || mCities.size() <=0) {
            if (cityItems != null){
                cityItems = new String[0];
                cityArrayWheelAdapter.setItems(cityItems);
            }
            return;
        }
        cityItems = new String[mCities.size()];
        int cityIndex = 0;
        for (int i = 0; i < mCities.size(); i++){
            cityItems[i] = mCities.get(i).name;
            if (UiUtils.isCompareCity(SELECTED_CITY, mCities.get(i).name)){
                cityIndex = i;
            }
        }
        cityArrayWheelAdapter.setItems(cityItems);
        wheelViewCity.setCurrentItem(cityIndex);
        SELECTED_CITY = cityItems[cityIndex];
    }
}
